package com.yl.service.impl;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.yl.common.core.domain.AjaxResult;
import com.yl.domain.ConferenceApplyDO;
import com.yl.domain.ConferenceDO;
import com.yl.domain.StudentReplyDO;
import com.yl.domain.dto.StudentDTO;
import com.yl.mapper.ConferenceApplyMapper;
import com.yl.mapper.ConferenceMapper;
import com.yl.service.ConferenceApplyService;
import com.yl.service.ZhiXingService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 会议申请报名表
 *
 * @author : y
 * @date :  2024/6/20 15:05
 **/
@Service
public class ConferenceApplyServiceImpl implements ConferenceApplyService {
    private static final Logger log = LoggerFactory.getLogger(ConferenceApplyServiceImpl.class);

    @Resource
    ConferenceApplyMapper conferenceApplyMapper;
    @Resource
    ConferenceMapper conferenceMapper;
    @Resource
    ZhiXingService zhiXingService;


    /**
     * 学生完成会议
     *
     * @param conferenceApplyDO
     * @author : y
     * @date :  2024/7/14 11:47
     */
    @Override
    public AjaxResult finish(ConferenceApplyDO conferenceApplyDO) {
        if (conferenceApplyDO.getConferenceId() == null) {
            return AjaxResult.error("会议id不能为空");
        }
        ConferenceApplyDO studentRe = conferenceApplyMapper.selectOne(Wrappers.<ConferenceApplyDO>lambdaQuery().eq(ConferenceApplyDO::getConferenceId, conferenceApplyDO.getConferenceId())
                .eq(ConferenceApplyDO::getId, conferenceApplyDO.getId()));
        ConferenceDO conferenceDO = conferenceMapper.selectById(conferenceApplyDO.getConferenceId());
        if (conferenceDO == null) {
            return AjaxResult.error("会议未查询到");
        }
        if (studentRe == null) {
            return AjaxResult.error("未查询到学生的会议信息");
        }
        // 更新会议状态
        if (conferenceDO.getConferenceStatus().equals("0")) {
            conferenceDO.setConferenceStatus("1");
            conferenceDO.setEndTime(new Date());
            conferenceMapper.updateById(conferenceDO);
        }
        if (studentRe.getSessionId() == null) {
            studentRe.setFinishTime(new Date());
            //学生开启职行面试
            List<StudentDTO> list = new ArrayList<>();
            StudentDTO dto = new StudentDTO();
            dto.setUser_id(studentRe.getId());
            dto.setUser_name(studentRe.getName());
            dto.setUser_phone(studentRe.getPhone());
            list.add(dto);
            /***批量给学生生成面试场次 **/
            AjaxResult createSession = zhiXingService.batchCreateSession(conferenceDO.getTopicId(), list);
            if (createSession != null && createSession.get("code").equals(200)) {
                // 新增成功
                JSONObject data = JSONObject.parseObject(createSession.get("data").toString());
                log.info("批量给学生生成面试场次，对象={}", data);
                List<Map<String, Object>> userList = (List<Map<String, Object>>) data.get("user_session");
                if (userList != null && userList.size() != 0) {
                    Map<String, Object> map = userList.get(0);
                    Long sessionId = Long.valueOf(map.get("session_id").toString());
                    log.info("本次创建获取的session_id为{}", sessionId);
                    studentRe.setSessionId(sessionId);
                    conferenceApplyMapper.updateById(studentRe);
                }
            } else {
                log.info("学生生成面试训练失败，对象={}", studentRe);
            }
        }
        return AjaxResult.success("操作成功");
    }
}
